package com.logos.cartoon.controller;

import com.logos.cartoon.entity.CharacterInfo;
import com.logos.cartoon.req.ChatReq;
import com.logos.cartoon.resp.CharacterInfoResp;
import com.logos.cartoon.result.Result;
import com.logos.cartoon.service.CharacterInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 角色信息表(CharacterInfo)表控制层
 *
 * @author rose
 * @since 2025-03-18 14:55:23
 */
@RestController
@RequestMapping("/characterInfo")
@Slf4j
public class CharacterInfoController {
    /**
     * 服务对象
     */
    @Resource
    private CharacterInfoService characterInfoService;

    /**
     * 查询所有数据
     */
    @GetMapping("/list")
    public Result<List<CharacterInfo>> queryAll() {
        try {
            List<CharacterInfo> list = characterInfoService.queryAll();
            return Result.ok(list);
        } catch (Exception e) {
            log.error("CharacterInfoController.queryAll.error:{}", e.getMessage(), e);
            return Result.fail("查询角色信息失败");
        }
    }

    /**
     * 根据id查询动漫角色
     */
    @GetMapping("/detail/{id}")
    public Result<CharacterInfoResp> queryById(@PathVariable Long id) {
        try {
            if (id == null) {
                return Result.fail("id不能为空");
            }
            CharacterInfoResp characterInfoResp = characterInfoService.queryById(id);
            return Result.ok(characterInfoResp);
        } catch (Exception e) {
            log.error("CharacterInfoController.queryById.error:{}", e.getMessage(), e);
            return Result.fail("查询角色信息失败");
        }
    }

    /**
     * 获取角色热度排行榜
     */
    @GetMapping("/hotRank")
    public Result<List<CharacterInfoResp>> getContributeList() {
        try {
            List<CharacterInfoResp> result = characterInfoService.hotRank();
            return Result.ok(result);
        } catch (Exception e) {
            log.error("SubjectController.getContributeList.error:{}", e.getMessage(), e);
            return Result.fail("获取贡献榜失败");
        }
    }

    /**
     * 搜索功能
     */
    @GetMapping("/search")
    public Result<List<CharacterInfoResp>> search(String keyword) {
        try {
            log.info("CharacterInfoController.search.keyword:{}", keyword);
            List<CharacterInfoResp> result = characterInfoService.search(keyword);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("CharacterInfoController.search.error:{}", e.getMessage(), e);
            return Result.fail("搜索失败");
        }
    }

    /**
     * 每日推荐动漫角色
     */
    @GetMapping("/random")
    public Result<CharacterInfoResp> random() {
        try {
            // 直接去redis中查
            log.info("CharacterInfoController.random");
            CharacterInfoResp result = characterInfoService.random();
            return Result.ok(result);
        } catch (Exception e) {
            log.error("CharacterInfoController.random.error:{}", e.getMessage(), e);
            return Result.fail("获取每日推荐失败");
        }
    }

    /**
     * 查询可以ai聊天的角色
     */
    @GetMapping("/chatList")
    public Result<List<CharacterInfoResp>> aiChat() {
        try {
            List<CharacterInfoResp> result = characterInfoService.chatList();
            return Result.ok(result);
        } catch (Exception e) {
            log.error("CharacterInfoController.aiChat.error:{}", e.getMessage(), e);
            return Result.fail("获取ai聊天角色列表失败");
        }
    }

    /**
     * ai聊天对话
     */
    @PostMapping("/chat")
    public Result<String> chat(@RequestBody ChatReq chatReq) {
        try {
            log.info("CharacterInfoController.chat.chatReq:{}", chatReq);
            String result = characterInfoService.chat(chatReq);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("CharacterInfoController.chat.chatReq:{}", e.getMessage(), e);
            return Result.fail("ai聊天对话失败");
        }
    }


}

